package com.example.wechat.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.R;
import com.example.wechat.model.FavoriteChat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class FavoritesChatAdapter extends RecyclerView.Adapter<FavoritesChatAdapter.ViewHolder> {
    private List<FavoriteChat> favoriteChats;
    private SimpleDateFormat dateFormat;

    public FavoritesChatAdapter(List<FavoriteChat> favoriteChats) {
        this.favoriteChats = favoriteChats;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_favorite_chat, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        FavoriteChat chat = favoriteChats.get(position);
        holder.nameTextView.setText(chat.getName());
        holder.contentTextView.setText(chat.getContent());
        holder.timeTextView.setText(dateFormat.format(new Date(chat.getTime())));
    }

    @Override
    public int getItemCount() {
        return favoriteChats.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameTextView;
        TextView contentTextView;
        TextView timeTextView;

        ViewHolder(View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.name_text);
            contentTextView = itemView.findViewById(R.id.content_text);
            timeTextView = itemView.findViewById(R.id.time_text);
        }
    }
} 